package com.shuoyutech.dp.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuoyutech.dp.cache.SerialNumberCaches;
import com.shuoyutech.dp.entity.HealthCheck;
import com.shuoyutech.dp.mapper.HealthCheckMapper;
import com.shuoyutech.dp.service.IHealthCheckService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shuoyuTech
 * @since 2021-02-25
 */
@Slf4j
@Service
public class HealthCheckServiceImpl extends ServiceImpl<HealthCheckMapper, HealthCheck> implements IHealthCheckService {

    @Autowired
    private HealthCheckMapper healthCheckMapper;
    @Autowired
    private SerialNumberCaches serialNumberCaches;

    @Override
    public HealthCheck checkHealth() {
        List<String> serialNumberList = serialNumberCaches.get("TEST");
        log.info("[cache] {}", JSON.toJSON(serialNumberList));
        if (CollectionUtils.isEmpty(serialNumberList)) {
            serialNumberCaches.put("TEST", Arrays.asList("001", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))));
        }
        return new HealthCheck().setCode(String.valueOf(healthCheckMapper.check())).setName("ok");
    }

    @Override
    public List<HealthCheck> checkJson() {
        List<HealthCheck> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            HealthCheck check = new HealthCheck();
            check.setId(IdWorker.getId());
            check.setName("json");
            check.setCode(i+"");
            list.add(check);
        }
        return list;
    }

}
